﻿#include "colorui.h"
#include "ui_colorUI.h"

#define MAXWIDTH 3
colorUI::colorUI(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::colorUI)
{
    ui->setupUi(this);

    // setWindowFlag(Qt::WindowStaysOnTopHint);
    setWindowFlags(Qt::WindowStaysOnTopHint|Qt::X11BypassWindowManagerHint);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setWindowFlags(windowFlags()&~Qt::WindowMinimizeButtonHint);

    ui->labelContrastValue->setText(QString("%0").arg(0,MAXWIDTH));
    ui->labelBrightnessValue->setText(QString("%0").arg(0,MAXWIDTH));
    ui->labelSaturationValue->setText(QString("%0").arg(0,MAXWIDTH));
    ui->labelGammaValue->setText(QString("%0").arg(0,MAXWIDTH));
}

colorUI::~colorUI()
{
    delete ui;
}

void colorUI::setWin(int x,int y,int w,int h,int winWidth,int winHeight){
    retInfo(QString("color %0 %1 %2 %3 win %4 %5").arg(x).arg(y).arg(w).arg(h).arg(winWidth).arg(winHeight));
    int posX = x, posY = y;
    if(x<0){
        posX = winWidth + x - w;
    }
    if(y<0){
        posY = winHeight + y - h;
    }
    retInfo(QString("color %0 %1").arg(posX).arg(posY));
    move(posX,posY);
    resize(w,h);
}

void colorUI::setContrast(int value){
    ui->horizontalSliderContrast->setValue(value);
}

void colorUI::setContrastLimit(int min,int max){
    ui->horizontalSliderContrast->setMinimum(min);
    ui->horizontalSliderContrast->setMaximum(max);
}

void colorUI::setBrightness(int value){
    ui->horizontalSliderBrightness->setValue(value);
}

void colorUI::setBrightnessLimit(int min,int max){
    ui->horizontalSliderBrightness->setMinimum(min);
    ui->horizontalSliderBrightness->setMaximum(max);
}

void colorUI::setSaturation(int value){
    if(value<0){
        ui->horizontalSliderSaturation->setEnabled(false);
    }else{
        ui->horizontalSliderSaturation->setValue(value);
    }
}

void colorUI::setSaturationLimit(int min,int max){
    ui->horizontalSliderSaturation->setMinimum(min);
    ui->horizontalSliderSaturation->setMaximum(max);
}

void colorUI::setGamma(int value){
    ui->horizontalSliderGamma->setValue(value);
}

void colorUI::setGammaLimit(int min,int max){
    ui->horizontalSliderGamma->setMinimum(min);
    ui->horizontalSliderGamma->setMaximum(max);
}

void colorUI::on_horizontalSliderContrast_valueChanged(int value)
{
    ui->labelContrastValue->setText(QString("%0").arg(value,MAXWIDTH));
    emit sendContrast(value);
}

void colorUI::on_horizontalSliderBrightness_valueChanged(int value)
{
    ui->labelBrightnessValue->setText(QString("%0").arg(value,MAXWIDTH));
    emit sendBrightness(value);
}

void colorUI::on_horizontalSliderSaturation_valueChanged(int value)
{
    ui->labelSaturationValue->setText(QString("%0").arg(value,MAXWIDTH));
    emit sendSaturation(value);
}

void colorUI::on_horizontalSliderGamma_valueChanged(int value)
{
    ui->labelGammaValue->setText(QString("%0").arg(value,MAXWIDTH));
    emit sendGamma(value);
}

void colorUI::closeEvent(QCloseEvent *event)
{
    // event->accept();
    event->ignore();
    emit sendExit();
}

void colorUI::moveEvent(QMoveEvent *event){
    retInfo(QString("moveEvent %0 %1 - %2 %3")
                .arg(event->pos().x()).arg(event->pos().y())
                .arg(this->pos().x()).arg(this->pos().y()));
    emit sendMove(this->pos().x(),this->pos().y());
}

void colorUI::resizeEvent(QResizeEvent *event){
    int w = event->size().width();
    int h = event->size().height();
    retInfo(QString("resizeEvent %0 %1").arg(w).arg(h));
    emit sendSize(w,h);
}

